package com.sky.task;

import com.sky.entity.Orders;
import com.sky.mapper.OrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;

@Component
public class OrderTask {
    @Autowired
    private OrderMapper orderMapper;


    /**
     * 超时未支付
     */
    //@Scheduled(cron = "0/5 * * * * ?")
    public void orderTimeOut() {
        //LocalDateTime localDateTime = LocalDateTime.now().plusMinutes(15);
        List<Orders>  ordersList = orderMapper.outTimeOut();
        if (!CollectionUtils.isEmpty(ordersList)){
            for (Orders orders : ordersList) {
                orders.setStatus(6);
                orders.setCancelTime(LocalDateTime.now());
                orders.setCancelReason("超时未支付");
                orderMapper.update(orders);
            }
        }
    }

    /**
     * 处理派送订单已完成
     */
    //@Scheduled(cron = "0 0 1 * * ?")
    public void orderDeliveryStatus() {
        List<Orders> ordersList = orderMapper.OrderDeliveryStatus();
        if (!CollectionUtils.isEmpty(ordersList)){
            for (Orders orders : ordersList) {
                orders.setStatus(5);
                orderMapper.update(orders);
            }
        }
    }
}
